﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Grinder.Renamer
{
    class Token
    {
        private readonly List<string> _arguments = new List<string>();
        private string _prefix = "";

        public string Prefix { get { return _prefix; } }
        public string Name { get; set; }
        public IList<string> Arguments { get { return _arguments; } }

        public Token(string token)
        {
            int prefixindex = token.IndexOf(RenamerConstants.PrefixDelimiter);

            string tokenstr;

            if (prefixindex != -1)
            {
                _prefix = token.Substring(0, prefixindex);
                tokenstr = token.Substring(prefixindex + 1);
            }
            else
                tokenstr = token;

            string[] split = tokenstr.Split(RenamerConstants.ArgumentDelimiter);

            Name = split[0];

            if (split.Length > 1)
            {
                string[] arguments = split[1].Split(RenamerConstants.MultiArgDelimiter);

                foreach (var arg in arguments)
                    _arguments.Add(arg);
            }
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder(RenamerConstants.LeftDelimiter.ToString());

            if (!string.IsNullOrEmpty(_prefix))
                sb.Append(_prefix + RenamerConstants.PrefixDelimiter);

            sb.Append(Name);

            if (_arguments.Count > 0)
            {
                sb.Append(RenamerConstants.ArgumentDelimiter);

                sb.Append(string.Join(RenamerConstants.MultiArgDelimiter.ToString(), Arguments.ToArray()));                
            }

            sb.Append(RenamerConstants.RightDelimiter);
            
            return sb.ToString();
        }
    }
}
